package org.jzentest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jzentest.dependency.ClassAnalyzer;
import org.jzentest.dependency.TestDependencies;

public class TestTracker {

	private ClassAnalyzer analyzer;
	private TestRunner testRunner;
	
	private Set<TestDependencies> testDependencies = new HashSet<TestDependencies>();

	public TestTracker(ClassAnalyzer analyzer, TestRunner runner) {
		this.analyzer = analyzer;
		this.testRunner = runner;
	}

	public void init(List<File> fileListingNoSort) throws FileNotFoundException, IOException {
		for(File f : fileListingNoSort) {
	        TestDependencies parse = analyzer.parse(f);
	        if (parse != null) {
	        	// this is the test
	        	testDependencies.add(parse);
	        }
			
		}
		for(TestDependencies t : testDependencies) {
			testRunner.runTest(t.getClassName());
		}
	}

	public void runTestsForFiles(List<File> changed) {
		Set<String> testsToRun = new HashSet<String>();
		for(File f : changed) {
			String filename = f.getAbsolutePath().substring(0, f.getAbsolutePath().length() - ".class".length());
			String test = findTest(filename);
			if (test != null) {
				testsToRun.add(test);
			}
		}
		
		for(String test : testsToRun) {
			testRunner.runTest(test);
		}
	}

	private String findTest(String filename) {
		// reanalize changed file
		// and find test for it
//		for(TestDependencies t : testDependencies) {
//			if (t.ge)
//		}
		// implement me
		System.out.print(".");
		System.out.print(".");
		System.out.print(".");
		System.out.println("Finding test to run for " + filename);
		return null;
	}

}
